import axios from 'axios'

import { getFullURL } from '@/utils/http'

import { httpConfig } from './config'

// 获取当前小程序环境
const Version = uni.getAccountInfoSync().miniProgram.envVersion

enum env_enum {
  release = 'production',
  trial = 'development',
  develop = 'base'
}

/**
 * 当前小程序环境
 */
const mini_env = env_enum[Version]

const instance = axios.create({
  adapter(config) {
    console.log('request adapter ↓↓')
    console.log(config)
    const { url, method, data, params, headers, baseURL, paramsSerializer } =
      config
    // Web 侧可以通过 vite.config.js 中的 proxy 配置，指定代理
    // 小程序APP里需写完整路径，如 https://service-rbji0bev-1256505457.cd.apigw.tencentcs.com/release
    // 可使用条件编译,详见 https://uniapp.dcloud.io/tutorial/platform.html#preprocessor
    // 目标baseUrl
    const targetUrl = baseURL || 'admin'
    /**
     * 请求baseUrl
     */
    let http_baseurl: string
    // #ifdef H5
    http_baseurl = (httpConfig as any)[import.meta.env.VITE_APP_AXIOS_BASE_URL][targetUrl]
    // #endif
    // #ifndef H5
    // @ts-ignore
    http_baseurl = httpConfig[mini_env][targetUrl]
    // #endif
    return new Promise((resolve, reject) => {
      uni.request({
        method: method!.toUpperCase() as any,
        url: getFullURL(http_baseurl || '', url!, params, paramsSerializer),
        header: headers,
        data,
        dataType: 'json',
        responseType: config.responseType,
        timeout: config.timeout || httpConfig.request_timeout,
        success: (res: any) => {
          console.log('request success ↓↓')
          console.log(res)
          resolve(res)
        },
        fail: (err: any) => {
          reject(err)
        }
      })
    })
  }
})

/**
 * 请求拦截
 */
instance.interceptors.request.use((config) => {
  const { method, params } = config
  // 附带鉴权的token
  const headers: any = {
    token: uni.getStorageSync('token')
  }
  // 不缓存get请求
  if (method === 'get') {
    headers['Cache-Control'] = 'no-cache'
  }
  // delete请求参数放入body中
  if (method === 'delete') {
    headers['Content-type'] = 'application/json;'
    Object.assign(config, {
      data: params,
      params: {}
    })
  }

  return {
    ...config,
    headers
  }
})

/**
 * 响应拦截
 */
instance.interceptors.response.use((v) => {
  if (v.data?.code === 401) {
    uni.removeStorageSync('token')
    // alert('即将跳转登录页。。。', '登录过期')
    // setTimeout(redirectHome, 1500)
    return v.data
  }

  // @ts-ignore
  if ((v.status || v.statusCode) === 200) {
    return v.data
  }
  // alert(v.statusText, '网络错误')
  return Promise.reject(v)
})

export default instance
